<?php
	include_once('PHPExcel/Classes/PHPExcel.php');
	class DeptController extends BaseController{

		protected  $_className ="Departs";

		public function createDepart($name,$parentid=1){
			
			$dept = new Department(AccessTokenManager::getAccessToken());
			$ret = json_decode($dept->createDepartment($name,$parentid));
			echo $ret->errcode;
			/*$arr = array(
				'id'   => $ret->id,
				'name' => $ret->name,
				'parentid' => $parentid
			);
			parent::save($dept);*/
		}

		public function batchImport($excel,$transaction){
			
			if(!empty($excel) &&count($excel)>0){
				
				  $objReader = PHPExcel_IOFactory::createReader('Excel2007');
		          $objReader->setReadDataOnly(true);
		          $objPHPExcel = $objReader->load($excel[0]["file_path"]);
		          $objPHPExcel->setActiveSheetIndex(0);
		          $objWorksheet = $objPHPExcel->getActiveSheet();         
		          $highestRow = $objWorksheet->getHighestRow(); 
		          $highestColumn = $objWorksheet->getHighestColumn(); 
		          $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
				  $deptList = array();		          
		          for ($row = 2; $row <= $highestRow; $row++) { 
		                $bean = AbstractBean::LoadBean("Shop");
		                $code = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
		                if($code ==  null ||  $code==""){
		                	continue;
		                }
		                if(!AbstractBean::UniqueValidate("Shop","code",$code)){
		                	throw new CustomerException("门店编号[".$code."]已经存在",3);
		                }
		                $bean->set("code",$code);
		                $bean->set("name",$objWorksheet->getCellByColumnAndRow(1, $row)->getValue());
		                $bean->set("company_name",$objWorksheet->getCellByColumnAndRow(2, $row)->getValue());
		                $bean->set("address",$objWorksheet->getCellByColumnAndRow(3, $row)->getValue());
		                $bean->set("zipcode",$objWorksheet->getCellByColumnAndRow(4, $row)->getValue());
		                $bean->set("contact",$objWorksheet->getCellByColumnAndRow(5, $row)->getValue());
		                $bean->set("contact_phone",$objWorksheet->getCellByColumnAndRow(6, $row)->getValue());
		                $bean->save();
		                
		         }

			}
		}



	}
?>